package com.xyz.cinema.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xyz.cinema.entity.Movie;
import com.xyz.cinema.entity.TableData;
import com.xyz.cinema.service.MovieService;
import com.xyz.cinema.util.DateUtil;

@Controller
@RequestMapping("movie")
public class MovieController {
	
	@Autowired
	MovieService movieService;
	
	@RequestMapping("selectAll")
	@ResponseBody
	public TableData selectAll(int page, int limit){
		//System.out.println(page);
		//System.out.println(limit);
		PageHelper.startPage(page,limit);
		List<Movie> list = movieService.selectAll();
		PageInfo<Movie> pageInfo = new PageInfo<Movie>(list, limit);
		TableData table = new TableData();
		table.setCode(0);
		table.setMsg("success");
		table.setCount(pageInfo.getTotal());
		table.setData(pageInfo.getList());
		//System.out.println(table);
		return table;
	}
	
	@RequestMapping("delete")
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException{
		int id = Integer.parseInt(request.getParameter("id"));
		String flag = "false";
		if(movieService.deleteMovie(id) > 0)
			flag = "true";
		PrintWriter pw = response.getWriter();
		pw.write(flag);
		pw.flush();
		pw.close();
	}
	
	@RequestMapping("selectById")
	public String selectById(HttpServletRequest request,HttpServletResponse response){
		System.out.println(request.getParameter("id"));
		int id = Integer.parseInt(request.getParameter("id"));
		Movie movie = movieService.selectById(id);
		request.setAttribute("movie", movie);
		return "movie";
	}
	
	@RequestMapping("movie")
	public String movie(HttpServletRequest request){
		if(request.getParameter("id") == "")
			return "forward:insert";
		else{
			return "forward:update";
		}
	}
	
	@RequestMapping("insert")
	public void insert(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String name = request.getParameter("name");
		Date onShelfDate = DateUtil.strToUtilDate(request.getParameter("onShelfDate"));
		Date offShelDate = DateUtil.strToUtilDate(request.getParameter("offShelfDate"));
		double ticketPrice = Double.parseDouble(request.getParameter("ticketPrice"));
		Movie movie = new Movie(0, name, onShelfDate, offShelDate, ticketPrice);
		String flag = "false";
		if(movieService.insertMovie(movie) > 0)
			flag = "true";
		PrintWriter pw = response.getWriter();
		pw.write(flag);
		pw.flush();
		pw.close();
	}
	
	@RequestMapping("update")
	public void update(HttpServletRequest request,HttpServletResponse response) throws IOException{
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		Date onShelfDate = DateUtil.strToUtilDate(request.getParameter("onShelfDate"));
		Date offShelDate = DateUtil.strToUtilDate(request.getParameter("offShelfDate"));
		double ticketPrice = Double.parseDouble(request.getParameter("ticketPrice"));
		Movie movie = new Movie(id, name, onShelfDate, offShelDate, ticketPrice);
		String flag = "false";
		if(movieService.updateMovie(movie) > 0)
			flag = "true";
		PrintWriter pw = response.getWriter();
		pw.write(flag);
		pw.flush();
		pw.close();
	}
	
}
